package cn.com.lsy.product.lepao.controller;

import cn.com.lsy.product.lepao.common.Constant;
import cn.com.lsy.product.lepao.controller.base.BaseController;
import cn.com.lsy.product.lepao.model.entities.LpAccessoryTbl;
import cn.com.lsy.product.lepao.model.entities.LpMessageTbl;
import cn.com.lsy.product.lepao.model.mt.entities.MessageInfoDto;
import cn.com.lsy.product.lepao.request.ActionLibraryInfoParam;
import cn.com.lsy.product.lepao.request.PushMessageParam;
import cn.com.lsy.product.lepao.request.SearchUserListParam;
import cn.com.lsy.product.lepao.request.UpdateMessageParam;
import cn.com.lsy.product.lepao.response.MessageInfoRet;
import cn.com.lsy.product.lepao.response.MessageListRet;
import cn.com.lsy.product.lepao.response.base.ApiResponseEnum;
import cn.com.lsy.product.lepao.response.base.BaseRet;
import cn.com.lsy.product.lepao.response.base.PageInfo;
import cn.com.lsy.product.lepao.service.IAccessoryService;
import cn.com.lsy.product.lepao.service.IEventService;
import cn.com.lsy.product.lepao.service.IJpushService;
import cn.com.lsy.product.lepao.service.IMessageService;
import cn.com.lsy.product.lepao.util.CommonFunctionUtil;
import cn.com.lsy.product.lepao.util.DozerMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(value = "消息管理接口", tags = "消息管理接口")
@RestController
@RequestMapping(value = "/lepao/console/doAuth")
public class MessageController extends BaseController {

    private Logger log = Logger.getLogger(MessageController.class);

    @Autowired
    IMessageService iMessageService;
    @Autowired
    IAccessoryService iAccessoryService;
    @Autowired
    IEventService iEventService;
    @Autowired
    IJpushService iJpushService;

    @RequestMapping(value = "/messageList", method = RequestMethod.POST)
    @ApiOperation(notes = "消息列表", httpMethod = "POST", value = "消息列表")
    public BaseRet<PageInfo<List<MessageListRet>>> messageList(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody SearchUserListParam param, BindingResult result) {

        BaseRet<PageInfo<List<MessageListRet>>> ret = new BaseRet<PageInfo<List<MessageListRet>>>();

        PageInfo<List<MessageListRet>> pageInfo = new PageInfo<List<MessageListRet>>();

        try {

            checkParam(result);

            List<MessageListRet> listRet = new ArrayList<MessageListRet>();

            int count = iMessageService.selectByCount(param.getStartTime(), param.getEndTime(), param.getSearchCondition());
            pageInfo.setCount(count);
            int start = param.getStart();

            List<LpMessageTbl> list = iMessageService.selectByPage(start, param.getPageSize(), param.getStartTime(), param.getEndTime(), param.getSearchCondition());

            if (list != null && list.size() > 0) {

                listRet = DozerMapperUtil.mapList(list, MessageListRet.class);
            }

            pageInfo.setRecords(listRet);

            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setData(pageInfo);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/messageInfo", method = RequestMethod.POST)
    @ApiOperation(notes = "消息详情", httpMethod = "POST", value = "消息详情")
    public BaseRet<MessageInfoRet> messageInfo(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param, BindingResult result) {

        BaseRet<MessageInfoRet> ret = new BaseRet<MessageInfoRet>();

        try {

            checkParam(result);

            MessageInfoDto info = iMessageService.selectMessageInfo(param.getId());

            MessageInfoRet infoRet = DozerMapperUtil.map(info, MessageInfoRet.class);

            ret.setMsg("查询成功");
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setData(infoRet);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/updateMessage", method = RequestMethod.POST)
    @ApiOperation(notes = "修改消息信息", httpMethod = "POST", value = "修改消息信息")
    public BaseRet<String> updateMessage(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody UpdateMessageParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpMessageTbl lpMessageTbl = new LpMessageTbl();
            lpMessageTbl.setId(param.getId());
            lpMessageTbl.setContent(param.getContent());
            lpMessageTbl.setTitle(param.getTitle());
            lpMessageTbl.setToUrl(param.getToUrl());
            lpMessageTbl.setType(param.getType());

            if (param.getIsUpdateImg().equals(Constant.getBoolean.yes)) {

                //写入新图片
                LpAccessoryTbl lpAccessoryTbl = new LpAccessoryTbl();
                lpAccessoryTbl.setInsertTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
                lpAccessoryTbl.setUrl(param.getImg());
                lpAccessoryTbl.setName("消息图片");

                iAccessoryService.insert(lpAccessoryTbl);
                lpMessageTbl.setImgId(lpAccessoryTbl.getId());

            }

            if (iMessageService.updateMessage(lpMessageTbl)) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("修改成功");

            } else {

                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
                ret.setMsg("修改失败");
            }


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/pushMessage", method = RequestMethod.POST)
    @ApiOperation(notes = "发送消息", httpMethod = "POST", value = "发送消息")
    public BaseRet<String> pushMessage(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody PushMessageParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            LpMessageTbl lpMessageTbl = new LpMessageTbl();
            lpMessageTbl.setContent(param.getContent());
            lpMessageTbl.setTitle(param.getTitle());
            lpMessageTbl.setToUrl(param.getToUrl());
            lpMessageTbl.setType(param.getType());
            lpMessageTbl.setInsertTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            if (StringUtils.isNotEmpty(param.getImg())) {
                //写入新图片
                LpAccessoryTbl lpAccessoryTbl = new LpAccessoryTbl();
                lpAccessoryTbl.setInsertTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
                lpAccessoryTbl.setUrl(param.getImg());
                lpAccessoryTbl.setName("消息图片");

                iAccessoryService.insert(lpAccessoryTbl);
                lpMessageTbl.setImgId(lpAccessoryTbl.getId());
            }

            if (iMessageService.insertMessage(lpMessageTbl)) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("发布成功");

                //异步处理消息数据
                iEventService.addUserMessage(lpMessageTbl.getId(), param.getPhoneList());

                //推送消息
                iJpushService.pushMessage(param.getPhoneList(), param.getTitle(), param.getExtra());

            } else {

                ret.setMsg("发布失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/delMessage", method = RequestMethod.POST)
    @ApiOperation(notes = "删除消息", httpMethod = "POST", value = "删除消息")
    public BaseRet<String> delMessage(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param, BindingResult result) {

        BaseRet<String> ret = new BaseRet<String>();

        try {

            checkParam(result);

            if (iMessageService.delMessage(param.getId())) {

                ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
                ret.setMsg("删除成功");

                //异步处理消息数据
                iEventService.delUserMessage(param.getId());

            } else {

                ret.setMsg("删除失败");
                ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            }


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }
}
